package com.hmyuan.controller.back;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Article;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.service.ArticleService;
import com.hmyuan.util.PageUtil;
import com.hmyuan.util.UserUtil;

/**
 * 页面controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/single")
public class SingleController {

	@Resource
	ArticleService articleService;
	@Resource
	PageUtil pageUtil;
	/**
	 * 页面管理页面
	 */
	@RequestMapping("/singlePage")
	public String singlePage(HttpServletRequest request,Model model){
		return "/back/single/singlePage";
	}
	/**
	 * 分页查询页面列表
	 */
	@RequestMapping("/findsingleList")
	public String findsingleList(HttpServletRequest request,Model model,Integer pageNum,Article article){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", articleService.selectListByPage(article, pageNum, 10));
		return "/back/single/singlePageList";
	}
	/**
	 * 页面新增页面
	 */
	@RequestMapping("/singleAddPage")
	public String singleAddPage(HttpServletRequest request,Model model){
		return "/back/single/singleAddPage";
	}
	/**
	 * 新增页面页面提交
	 */
	@ResponseBody
	@RequestMapping("/addsingle")
	public String addsingle(HttpServletRequest request,Model model,
			Article article,
			String url
	){
		/*获取当前用户*/
		Sys_user currentUser = UserUtil.getCurrentUser(request);
		/*页面初始化*/
		if(article.getType()!=100){
			article.setTopimg(url);
		}
		article.setUserid(currentUser.getId());
		article.setUseraccount(currentUser.getAccountname());
		article.setCreatetime(new Date());
		article.setClicknum(0l);
		articleService.insert(article);
		//更新页面左侧列表
		pageUtil.headPageListInitSingle();
		return "success";
	}
	/**
	 * 编辑页面信息页面
	 */
	@RequestMapping("/singleUpdatePage")
	public String singleUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/single/singleAddPage";
		Article article = articleService.selectByPrimaryKey(id);
		request.setAttribute("article", article);
		return "/back/single/singleUpdatePage";
	}
	/**
	 * 编辑页面页面提交
	 */
	@ResponseBody
	@RequestMapping("/updatesingle")
	public String updatesingle(HttpServletRequest request,Model model,
			Article article,
			String url
	){
		if(null==article.getId()) return "fail";
		
		if(article.getType()!=100){
			article.setTopimg(url);
		}
		articleService.updateByPrimaryKeySelective(article);
		//更新页面左侧列表
		pageUtil.headPageListInitSingle();
		return "success";
	}
	/**
	 * 根据id删除页面
	 */
	@ResponseBody
	@RequestMapping("/deletesingleById")
	public String deletesingleById(HttpServletRequest request,Model model,Long id){
		if(null==id) return "fail";
		articleService.deletearticleById(id);
		//更新页面左侧列表
		pageUtil.headPageListInitSingle();
		return "success";
	}
	
}
